<?php

namespace app\controller;

use app\service\UserService;
use library\tencent_map;
use think\facade\Db;

class UserController extends Base
{
    protected array $noNeedLogin = ['get_qrcode_details'];

    //获取用户信息
    public function info()
    {
        $res = UserService::info($this->user);
        return json(apiSuccess($res));
    }

    //变更微信消息通知状态
    public function change_wx_notice()
    {
        $wx_notice = $this->request->post('wx_notice');
        $res = (new UserService())->change_wx_notice($this->user, $wx_notice);
        return json($res);
    }


    //修改小程序码
    public function update_qrcode()
    {
        $from = $this->request->post('from');
        $res = (new UserService())->update_qrcode($this->user, $from);
        return json($res);
    }


    //获取该小程序码详情
    public function get_qrcode_details()
    {
        $qrcode_id = $this->request->get('qrcode_id');
        $res = (new UserService())->get_qrcode_details($qrcode_id);
        return json($res);
    }


    //发送订阅消息
    public function send_subscribe_message()
    {

        $send_msg = $this->request->post('send_msg');
        $phone = $this->request->post('phone');
        $lng = $this->request->post('lng');
        $lat = $this->request->post('lat');
        $to_user_id = $this->request->post('to_user_id');

        $res = (new tencent_map())->geocoder($lat, $lng);
        if (empty($address)) {
            $address = $res['result']['address'] ?? '未知';
        }

        if (empty($send_msg)) {
            return json(apiError('推送消息不能为空~'));
        }

        if (empty($phone)) {
            $phone = '对方未留下联系方式';
        }

        $send_data = [
            'address' => $address,
            'send_msg' => $send_msg,
            'lat' => $lat,
            'lng' => $lng,
            'phone' => $phone,
            'from_user_id' => $this->user_id,
            'to_user_id' => $to_user_id
        ];
        $res = (new UserService())->send_subscribe_message($this->user, $send_data);
        return json($res);
    }


    //绑定挪车码
    public function bind_qrcode()
    {
        $from = $this->request->post('from');
        $res = (new UserService())->bind_qrcode($this->user, $from);
        return json($res);
    }


    //解绑挪车码
    public function unbind_qrcode()
    {

        $qrcode_id = $this->request->post('qrcode_id');

        // 启动事务
        try {
            $res = Db::transaction(function () use ($qrcode_id) {
                $res = (new UserService())->unbind_qrcode($this->user, $qrcode_id);
                if (empty($res['code']) || $res['code'] !== 1) {
                    throw new \Exception($res['msg']);
                }
                return $res;
            });

            return json($res);

        } catch (\Throwable $e) {
            return json(apiError($e->getMessage()));
        }

    }

    //修改昵称和头像
    public function update_info()
    {
        $nickname = $this->request->post('nickname');
        $avatar = $this->request->post('avatar');
        $res = (new UserService())->update_info($this->user, $nickname, $avatar);
        return json($res);
    }

    public function bind_phone()
    {
        $phone = $this->request->post('phone');
        $res = (new UserService())->bind_phone($this->user, $phone);
        return json($res);
    }

    //生成体验码
    public function create_experience_qrcode()
    {
        $from = $this->request->post('from');
        $res = (new UserService())->create_experience_qrcode($this->user, $from);
        return json($res);
    }

    //解绑体验码
    public function unbind_experience_qrcode()
    {
        $res = (new UserService())->unbind_experience_qrcode($this->user);
        return json($res);
    }

    //通过参数获取体验码信息
    public function get_test_qrcode_details()
    {

        $other_user_id = $this->request->get('user_id');
        $res = (new UserService())->get_test_qrcode_details($other_user_id);
        return json($res);
    }


    //获取微信地图位置
    public function get_wx_map()
    {
        $message_id = $this->request->get('message_id');
        $res = (new UserService())->get_wx_map($this->user, $message_id);
        return json($res);
    }


    //获取奖励列表
    public function get_reward_list()
    {
        $limit = $this->request->get('limit', 10);
        $status = $this->request->get('status', 0);
        $res = (new UserService())->get_reward_list($this->user, $status, $limit);
        return json($res);
    }

    //用户申请提现
    public function apply_withdraw()
    {
        // 启动事务
        $id = $this->request->post('id');
        $res = (new UserService())->apply_withdraw($this->user, $id);
        return json($res);


    }

    //确认收款
    public function confirm_reward()
    {
        // 启动事务
        $id = $this->request->post('id');
        $res = (new UserService())->confirm_reward($this->user, $id);
        return json($res);


    }

    //统计
    public function statistics()
    {
        $res = (new UserService())->statistics($this->user);
        return json($res);
    }


    //成员列表
    public function member_list()
    {

        $date = $this->request->get('date');
        if (empty($date)) {
            $date = date('Y-m');
        }

        //这个月的最后一天 精确到秒
        $date = date('Y-m-t 23:59:59', strtotime($date));

        $limit = $this->request->get('limit', 10);
        $res = (new UserService())->member_list($this->user, $date, $limit);
        return json($res);
    }


    //设置扫码记录
    public function set_scan_record()
    {
        $pid = $this->request->post('pid');
        $type = $this->request->post('type');
        $qrcode = $this->request->post('qrcode');

        $res = (new UserService())->set_scan_record($this->user, $pid, $type, $qrcode);
        return json($res);
    }


}